package com.demo2do.fms.admin.dao.supplier;

import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.demo2do.fms.entity.authority.Organization;

/**
 * 
 * @author Wy
 *
 */
@Dao("hibernate")
public interface OrganizationDao {
    
    /**
     * @param id
     * @return
     */
    @Query("FROM Organization WHERE id = :id AND  disabled = false")
    public Organization load(@Variable("id") Long id);
    
    /**
     * 
     * @param code
     * @return
     */
    @Query("FROM Organization WHERE code = :code AND disabled = false")
    public Organization load(@Variable("code") String code);
    
    /**
     * @param code
     * @param system
     * @return
     */
    @Query("FROM Organization WHERE code = :code AND system = :system AND internal IN (0,1) AND disabled = false")
    public Organization load(@Variable("code") String code, @Variable("system") Boolean system);
    
    /**
     * @param ids
     * @return
     */
    @Query("FROM Organization WHERE id IN ( :ids ) AND  disabled = false")
    public List<Organization> list(@Variable("ids") List<Long> ids);
    
    /**
     * list all organizations
     * 
     * @param system
     * @return
     */
    @Query("FROM Organization WHERE system = :system and internal IN (0,1) and disabled = false")
    public List<Organization> list(@Variable("system") Boolean system);

    /**
     * count organization by system
     * 
     * @param system
     * @return
     */
    @Query("SELECT COUNT(*) FROM Organization WHERE system = :system and internal IN (0,1) and disabled = false")
    public int count(@Variable("system") Boolean system);
    
    /**
     * list organizations by system and beginIndex and maxResult
     * 
     * @param system
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM Organization WHERE system = :system and internal IN (0,1) and disabled = false")
    public List<Organization> list(@Variable("system") Boolean system, @Variable("beginIndex")int beginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * list organization ids by system
     * 
     * @return
     */
    @Query("SELECT organization.id FROM Organization organization WHERE organization.system = false")
    public List<Long> listOutSystemOrgIds();
    
}
